<template>
  <el-container class="home-container">
    <!--Header-->
    <el-header>
      <div>
        <img src="@/assets/img/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--Container-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单-->
        <el-menu
                background-color="#333744"
                text-color="#fff"
                active-text-color="#409eff"
                unique-opened
                :collapse="isCollapse"
                :collapse-transition="false"
                router :default-active="activePath">
          <!--一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!--一级模板-->
            <template slot="title">
              <!--图标-->
              <i :class="iconObj[item.id]"></i>
              <!--文本-->
              <span>{{item.authName}}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="'/' + subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id" @click="saveNavState($route.path)">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--Main-->
      <el-main>
        <!--welcome 路由占位-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    name: "Home",
    data() {
      return {
        // 左侧菜单数据
        menuList: [],
        iconObj: {
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-tijikongjian',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao',
        },
        // 侧边栏是否折叠
        isCollapse: false,
        activePath: ''
      }
    },
    created() {
      this.getMenuList()
      this.activePath = sessionStorage.getItem('activePath')
    },
    methods: {
      logout() {
        // 指定清空token  clear()慎用
        sessionStorage.removeItem('token')
        // 跳转登录页
        this.$router.replace('/login')
      },
      getMenuList() {
        this.$http.get('menus').then(res => {
          if (res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg)
          this.menuList = res.data.data
        })
      },
      toggleCollapse() {
        this.isCollapse = !this.isCollapse
      },
      saveNavState(activePath) {
        sessionStorage.setItem('activePath', activePath)
        this.activePath = activePath
      }
    }
  }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
    overflow: hidden;
  }

  .el-header {
    background-color: #373d41;
    color: #fff;
    font-size: 20px;
    align-items: center;
    padding-left: 0;
    display: flex;
    justify-content: space-between;

    > div {
      display: flex;
      align-items: center;

      span {
        margin-left: 10px;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    color: #333;

    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: #EAEDF1;
    color: #333;
    /*chrome下overflow有个新的属性值overlay,在触发滚动条时候并不挤压空间*!*/
    overflow-y: overlay;
    /*overflow: auto;*/
    height: 680px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .iconfont {
    margin-right: 10px;
  }

  .toggle-button {
    background-color: #4a5064;
    color: #ffffff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
